package com.example.springboot.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.springboot.entity.User;
import com.example.springboot.mapper.MessageMapper;
import com.example.springboot.mapper.UserMapper;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;
import com.example.springboot.common.Result;
import jakarta.annotation.Resource;

import java.util.Date;
import java.util.List;

import com.example.springboot.entity.Message;

import org.springframework.web.bind.annotation.RestController;

import static org.springframework.messaging.simp.SimpMessageHeaderAccessor.getUser;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author luo
 * @since 2023-07-22
 */
@RestController
@RequestMapping("/message")
public class MessageController extends BaseController{
    @Resource
    private MessageMapper messageMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    HttpServletRequest request;

    @PostMapping
    public Result save(@RequestBody Message Message) {
        Message.setUsername(getUser().getUsername());
        Message.setTime(DateUtil.formatDateTime(new Date()));
        return Result.success(messageMapper.insert(Message));
    }

    // 查询所有数据
    @GetMapping("/foreign/{id}")
    public Result foreign(@PathVariable Integer id) {
        return Result.success(findByForeign(id));
    }

    private List<Message> findByForeign(Integer foreignId) {
        // 根据 foreignId 0 查询所有的留言数据
        LambdaQueryWrapper<Message> queryWrapper = Wrappers.<Message>lambdaQuery().eq(Message::getForeignId, foreignId).orderByDesc(Message::getId);
        List<Message> list = messageMapper.selectList(queryWrapper);
        // 循环所有留言数据
        for (Message Message : list) {
            User one = userMapper.selectOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, Message.getUsername()));
            if (StrUtil.isNotBlank(one.getAvatarUrl())) {
                Message.setAvatar(one.getAvatarUrl());
                Message.setNickname(one.getNickname());
            } else {
                // 默认一个头像
                Message.setAvatar("https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png");
            }
            Long parentId = Message.getParentId();
            // 判断当前的留言是否有父级，如果有，则返回父级留言的信息
            // 原理：遍历所有留言数据，如果id跟当前留言信息的parentId相等，则将其设置为父级评论信息，也就是Message::setParentMessage
            list.stream().filter(c -> c.getId().equals(parentId)).findFirst().ifPresent(Message::setParentMessage);
        }
        return list;
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id) {
        messageMapper.deleteById(id);
        return Result.success();
    }


}

